﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AdventureWorksWebApp
{
    public partial class list : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["search"]))
            {
                using (DataClasses1DataContext dc = new DataClasses1DataContext())
                {
                    var result = from prod in dc.Products
                                           .Where(y => y.Name.ToLower().Contains(Request.QueryString["search"]))
                                 from subcat in dc.ProductSubcategories
                                 .Where(v => v.ProductSubcategoryID == prod.ProductSubcategoryID)
                                 .DefaultIfEmpty()
                                 from cat in dc.ProductCategories
                                 .Where(c => c.ProductCategoryID == subcat.ProductCategoryID)
                                 .DefaultIfEmpty()
                                 from photo in dc.ProductProductPhotos
                                 .Where(p => p.ProductID == prod.ProductID)
                                 .DefaultIfEmpty()
                                 from photothumb in dc.ProductPhotos
                                 .Where(pt => pt.ProductPhotoID == photo.ProductPhotoID)
                                 .DefaultIfEmpty()
                                 select new
                                 {
                                     productID = prod.ProductID,
                                     productName = prod.Name,
                                     productPrice = prod.ListPrice,
                                     subcategoryName = prod.ProductSubcategory.Name,
                                     categoryName = prod.ProductSubcategory.ProductCategory.Name,
                                     photo = photothumb.ThumbNailPhoto
                                 };
                    GridView1.DataSource = result;
                    GridView1.DataBind();

                    if (result.Count() == 0)
                        lblNoRows.Visible = true;
                    else
                        lblNoRows.Visible = false;
                }
            }
            else
            {
                using (DataClasses1DataContext dc = new DataClasses1DataContext())
                {
                    GridView1.DataSource = from prod in dc.Products
                                           from subcat in dc.ProductSubcategories
                                           .Where(v => v.ProductSubcategoryID == prod.ProductSubcategoryID)
                                           .DefaultIfEmpty()
                                           from cat in dc.ProductCategories
                                           .Where(c => c.ProductCategoryID == subcat.ProductCategoryID)
                                           .DefaultIfEmpty()
                                           from photo in dc.ProductProductPhotos
                                           .Where(p => p.ProductID == prod.ProductID)
                                           .DefaultIfEmpty()
                                           from photothumb in dc.ProductPhotos
                                           .Where(pt => pt.ProductPhotoID == photo.ProductPhotoID)
                                           .DefaultIfEmpty()
                                           select new
                                           {
                                               productID = prod.ProductID,
                                               productName = prod.Name,
                                               productPrice = prod.ListPrice,
                                               subcategoryName = prod.ProductSubcategory.Name,
                                               categoryName = prod.ProductSubcategory.ProductCategory.Name,
                                               photo = photothumb.ThumbNailPhoto
                                           };

                    GridView1.DataBind();
                }
            }
            Page.DataBind();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataBind();
        }

        protected void lnkSzukaj_Click(object sender, EventArgs e)
        {
            Response.Redirect("list.aspx?search=" + TextBox1.Text);
        }
    }
}